import pygame

import common


class BaseSprite(pygame.sprite.Sprite):

    containers = []

    def __init__(self, pos, image=None):
        pygame.sprite.Sprite.__init__(self, *self.containers)
        self.pos = pos
        if image is not None:
            self.image = common.load_image(image)
            self.rect = self.image.get_rect(midbottom=pos)

    @classmethod
    def add_groups(cls, *groups):
        cls.containers.extend(list(groups))


class BaseSpriteGroup(pygame.sprite.Group):
    pass


class BaseRenderUpdates(pygame.sprite.RenderUpdates):
    pass


class Ship(BaseSprite):

    containers = []

    def __init__(self, pos):
        BaseSprite.__init__(self, pos, 'spaceship1.png')
        self.reloading = 0

    def move(self, direction):
        self.rect.move_ip(direction*5, 0)

    @property
    def gunpos(self):
        return self.rect.centerx, self.rect.top


class Block(BaseSprite):

    containers = []

    def __init__(self, pos, speed):
        BaseSprite.__init__(self, pos, 'block.png')
        self.speed = speed

    def update(self):
        self.rect.move_ip(0, self.speed)


class Bullet(BaseSprite):

    containers = []

    def __init__(self, pos):
        BaseSprite.__init__(self, pos, 'bullet.png')

    def update(self):
        self.rect.move_ip(0, -3)


class Score(BaseSprite):

    containers = []

    def __init__(self, level):
        BaseSprite.__init__(self, (400, 10))
        self.level = level
        self.font = pygame.font.Font(None, 20)
        self.color = pygame.locals.Color('white')
        self.current_score = -1
        self.update()
        self.rect = self.image.get_rect().move(self.pos)

    def update(self):
        if self.level.score == self.current_score:
            return
        self.current_score = self.level.score
        self.image = self.font.render("Score: %d" % self.current_score, 0, self.color)

